﻿<?php
	include '/inc/conn.php';
	
	session_start();
	extract($_POST);
	extract($_GET);
	$pagestr = end(explode("/",$_SERVER['PHP_SELF']));
	unset($_POST,$_GET);
	
	if (isset($act))
	{
		if ($act == 'add')
		{
			if (empty($pcpn) || 	  
				empty($script) ||	  
				empty($imgpath) ||  
				empty($ssd1) || 	
				empty($ssd2) ||		
				empty($hdd)){
				echo "<script>alert('所填内容有空！！！');window.location='$pagestr';</script>";
				die("所填内容有空！！！!");
			}

			$pcpn 	 =trim(strtoupper($pcpn));
			$script	 =trim(strtoupper($script));
			$imgpath =trim(strtoupper($imgpath));
			$imgtype =trim(strtoupper($imgtype));
			$ssd1 	 =trim(strtoupper($ssd1));
			$ssd2 	 =trim(strtoupper($ssd2));
			$hdd 	 =trim(strtoupper($hdd));
			
			if (strlen($pcpn)!=18){
				echo "<script>alert('成品料号长度不对！');window.location='$pagestr';</script>";
				die("SSID长度不对!");
			}

			$db = new mysql();
			$db->select_db('image');
			$sql="INSERT INTO `imagelist` (`IMG_TYPE`,`HAS_SSD2`,`PC_PN`,`IMG_PATH`,`IMG_SCRIPT`,`HAS_SSD1`,`HAS_HDD`) values('$imgtype', '$ssd2', '$pcpn', '$imgpath', '$script' ,'$ssd1', '$hdd' )";
			$db->query($sql);
			echo "<script>alert('数据添加成功!');window.opener.location.reload();window.location.href='$pagestr'</script>";
			$db->close();
		}
		
		if ($act == 'change')
		{
			if (empty($pcpn) || 	  
				empty($script) ||	  
				empty($imgpath) ||  
				empty($ssd1) ||
				empty($ssd2) ||
				empty($hdd)){
				echo "<script>alert('所填内容有空！！！');window.location='$pagestr';</script>";
				die("所填内容有空！！！!");
			}

			$pcpn 	 =trim(strtoupper($pcpn));
			$script	 =trim(strtoupper($script));
			$imgpath =trim(strtoupper($imgpath));
			$imgtype =trim(strtoupper($imgtype));
			$ssd1 	 =trim(strtoupper($ssd1));
			$ssd2 	 =trim(strtoupper($ssd2));
			$hdd 	 =trim(strtoupper($hdd));
		
			if (strlen($pcpn)!=18){
				echo "<script>alert('成品料号长度不对！');window.location='$pagestr';</script>";
				die("SSID长度不对!");
			}

			$db = new mysql();
			$db->select_db('image');
			$table = "imagelist";
			if(isset($isnew)){
				$db->query("select * from imagelist where `PC_PN`='$pcpn'");
				$num1 = $db->db_num_rows();
				if($num1==0){
					$sql="INSERT INTO `imagelist` (`IMG_TYPE`,`HAS_SSD2`,`PC_PN`,`IMG_PATH`,`IMG_SCRIPT`,`HAS_SSD1`,`HAS_HDD`) values('$imgtype', '$ssd2', '$pcpn', '$imgpath', '$script' ,'$ssd1', '$hdd')";
					$db->query($sql);
					echo "<script>alert('插入新资料完成!');window.location.href='$pagestr';</script>";
				}else{
					echo "<script>alert('数据已经存在!');window.location.href='$pagestr';</script>";
				}
			}else{
				$condition = "ID=${id}";
				$mod_content="`IMG_TYPE`='${imgtype}', `HAS_SSD2`='${ssd2}', `PC_PN`='$pcpn', `IMG_SCRIPT`='${script}', `IMG_PATH`='${imgpath}', `HAS_SSD1`='${ssd1}', `HAS_HDD`='${hdd}'";
				$db->update($table, $mod_content, $condition);
				echo "<script>alert('内容修改完成!');window.location.href='$pagestr';</script>";
			}
			$db->close();
		}
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-migrate.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script>
	$(function(){
		$("#ADDDIV").hide();
		$("#ADDIMG").click(function(){
			$("#ADDDIV").toggle();
		});
	});
</script>
<title>出货系统管理</title>
</head>
<body class="background">
<div class="container white-background pagecontent">
<?
		echo "<div id='ADDDIV'>\n";
		echo "<form action='$pagestr?act=add' method='post'>\n";
		echo "	<div>\n";
		echo "	添加出货信息:<br />\n";
		echo "		成品料号: <input name='pcpn' id='pcpn' style='width:180'><br><br>\n";
		echo "		IMAGE路径: <input name='imgpath' id='imgpath' style='width:180'><br><br>\n";
		echo "		Script名称: <input name='script' id='script' style='width:180'><br><br>\n";
		echo "		IMAGE类型: <input name='imgtype' id='imgtype' style='width:180'><br><br>\n";
		echo "		SSD1信息: <input name='ssd1' id='ssd1' style='width:180'><br><br>\n";
		echo "		SSD2信息: <input name='ssd2' id='ssd2' style='width:180'><br><br>\n";
		echo "		HDD信息: <input name='hdd' id='hdd' style='width:180'><br><br>\n";
		echo "	</div>\n";
		echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit'>\n<input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
		echo "</form>\n";
		echo "</div>\n";

		// change item page
		if ($pagename=="changeitem"){
		$db = new mysql();
		$sql="select * from image.imagelist where ID=$id";
		$db->query($sql);
		$num = $db->db_num_rows();
		$row = $db->fetch_assoc();
		echo "\n<form action='$pagestr?act=change&id=${row['ID']}' method='post'>\n";
		echo "	<div>\n";
		echo "	修改出货信息:<br />\n";
		echo "		成品料号: <input name='pcpn' id='pcpn' style='width:180' value='${row['PC_PN']}'><br><br>\n";
		echo "		IMAGE路径: <input name='imgpath' id='imgpath' style='width:180' value='${row['IMG_PATH']}'><br><br>\n";
		echo "		Script名称: <input name='script' id='script' style='width:180' value='${row['IMG_SCRIPT']}'><br><br>\n";		
		echo "		IMAGE类型: <input name='imgtype' id='imgtype' style='width:180' value='${row['IMG_TYPE']}'><br><br>\n";
		echo "		SSD1信息: <input name='ssd1' id='ssd1' style='width:180' value='${row['HAS_SSD1']}'><br><br>\n";
		echo "		SSD2信息: <input name='ssd2' id='ssd2' style='width:180' value='${row['HAS_SSD2']}'><br><br>\n";
		echo "		HDD信息: <input name='hdd' id='hdd' style='width:180' value='${row['HAS_HDD']}'><br><br>\n";
		echo "		<font color='red'>加入新的？</font>: <input type='checkbox' name='isnew' value='1' /><br><br>\n";
		echo "	</div>\n";
		echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit'>\n";
		echo "  <input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
		echo "</form>\n";
		$db->close();
	}else{
		echo "<label>操作项次: </label><a id='ADDIMG' href='#'> 添加</a>";
		$db = new mysql();
		if(isset($sortby)){
			switch($sortby){
				case "sp":
					$db->query("select * from image.imagelist order by PC_PN asc");
					break;
				default:
					$db->query("select * from image.imagelist order by IMG_PATH desc");
			}
		}else{
			$db->query("select * from image.imagelist order by UPDATE_TIME desc");
		}
		$num = $db->db_num_rows();
		echo "\n<table border=0 cellpadding=0 class='table table-hover'>\n";
		echo "<tr><th>Item</th><th><a href='$pagestr?sortby=sp'>成品料号</a></th><th><a href='$pagestr?sortby=504'>存放路径</a></th><th>脚本名称</th><th>IMAGE类型</th><th>SSD1信息</th><th>SSD2信息</th><th>HDD信息</th><th>操作</th></tr>\n";
		for($i=0; $i<$num; $i++){
			$row = $db->fetch_assoc();
			$item = $i + 1;
			echo "<tr>";
			echo "<td>$item</td>";
			if(substr($row['ADD_TIME'],0,10)== date("Y-m-d")){
				echo "<td><font color='blue'>${row['PC_PN']}</font></td>";
				echo "<td><font color='blue'>${row['IMG_PATH']}</font></td>";
			}else{
				echo "<td>${row['PC_PN']}</td>";
				echo "<td>${row['IMG_PATH']}</td>";				
			}
			echo "<td>${row['IMG_SCRIPT']}</td>";
			echo "<td>${row['IMG_TYPE']}</td>";
			echo "<td>${row['HAS_SSD1']}</td>";
			echo "<td>${row['HAS_SSD2']}</td>";
			echo "<td>${row['HAS_HDD']}</td>";
			echo "<td><a href='$pagestr?pagename=changeitem&id=${row['ID']}'>改</a></td>";
			echo "</tr>\n";
		}
		echo "\n</table>";
		$db->close();
	}
?>
</div>
</body>
</html>
